import { ElMessage, ElMessageBox } from 'element-plus'
export default function () {
  // 表格数组对象
  const tableData = ref([{ a: 1 }])
  // 表格配置
  const tableConfig = ref({})
  // 表头对象
  const tableHeaderForm = ref({})
  // 分页对象
  const pageInfo = reactive({
    pageNum: 1,
    total: 100,
    pageSize: 20
  })

  // 表头重置
  const reset = () => {
    return new Promise((resolve) => {
      tableHeaderForm.value = {}
      pageInfo.pageNum = 1
      resolve()
    })
  }
  // 弹窗控制器
  const dialogVisible = ref(false)

  // 弹窗综合对象
  const dialogTypeObj = reactive({
    visible: false,
    type: 0,
    form: {}
  })

  // 弹窗提示
  const commonSubmit = ({
    text = '提示信息',
    title = '提示',
    confirmBtn = '确认',
    cancelBtn = '取消',
    status = 'warning'
  }) => {
    return new Promise((resolve) => {
      ElMessageBox.confirm(text, title, {
        confirmButtonText: confirmBtn,
        cancelButtonText: cancelBtn,
        type: status
      }).then(() => {
        resolve()
      })
    })
  }

  // 纯浪费时间
  const headerBind = (defaultObj = {}) => {
    return {
      form: tableHeaderForm.value,
      gap: '10',
      allAlign: 'flex-start',
      ...defaultObj
    }
  }

  const headerOn = (getTableData = function () {}) => {
    return {
      reset: function () {
        reset().then((e) => {
          getTableData()
        })
      },
      dateChange: function () {
        [tableHeaderForm.value.startDate, tableHeaderForm.value.endDate] = [
          tableHeaderForm.value.dateRange[0],
          tableHeaderForm.value.dateRange[1]
        ]
      },
      search: function () {
        return new Promise((resolve) => {
          pageInfo.pageNum = 1
          resolve()
        }).then((e) => {
          getTableData()
        })
      }
    }
  }

  // 纯浪费时间
  const tableBind = (defaultObj = {}) => {
    return {
      minTableHeight: 140,
      minusPart: 210,
      tableData: tableData.value,
      ...defaultObj
    }
  }

  return {
    tableData,
    tableConfig,
    tableHeaderForm,
    pageInfo,
    dialogVisible,
    dialogTypeObj,
    reset,
    commonSubmit,
    headerBind,
    headerOn,
    tableBind
  }
}
